/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comparators;

import entities.Country;
import interfaces.InternationalizableEntity;
import java.util.Comparator;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;

/**
 *
 * @author JodeteBrazo
 */
public class InternationalizableEntityComparator implements Comparator<InternationalizableEntity> {
    
    private Locale locale;
    
    public InternationalizableEntityComparator(){
        locale= FacesContext.getCurrentInstance().getViewRoot().getLocale();
    }
    
    public InternationalizableEntityComparator(String languageCode){
        locale= new Locale(languageCode);
    }
    
    public InternationalizableEntityComparator(Locale locale){
        this.locale=locale;
    }
    
    @Override
    public int compare(InternationalizableEntity e1, InternationalizableEntity e2) {
        String fullQualifiedClassName=e1.getClass().getCanonicalName();
        String[] stringArray;
        stringArray=fullQualifiedClassName.split("\\.");
        String className=stringArray[stringArray.length-1];
        String bundleName="bundle."+className+"Bundle";
        ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
        String e1Description=rb.getString(e1.getId().toString());
        String e2Description=rb.getString(e2.getId().toString());
        return e1Description.compareTo(e2Description);
    }
    
}
